package com.timebank.volunteer.listener;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.timebank.common.core.constant.SecurityConstants;
import com.timebank.common.core.constant.TimeCoinTradeStatusConstant;
import com.timebank.common.core.constant.rabbitmq.queue.QueueConstant;
import com.timebank.common.core.exception.ServiceException;
import com.timebank.common.core.result.Result;
import com.timebank.common.core.utils.StringUtils;
import com.timebank.common.core.web.pojo.AjaxResult;
import com.timebank.system.model.LoginUser;
import com.timebank.system.pojo.SysUser;
import com.timebank.system.service.RemoteUserService;
import com.timebank.timecoin.pojo.entity.TimeCoinTrade;
import com.timebank.timecoin.pojo.entity.User;
import com.timebank.timecoin.service.RemoteContractService;
import com.timebank.timecoin.service.RemoteTimeCoinService;
import com.timebank.common.core.constant.message.MessageTypeConstant;
import com.timebank.volunteer.constant.SenderAndGetterConstant;
import com.timebank.volunteer.constant.URLConstant;
import com.timebank.volunteer.mapper.VolunteerAttendListMapper;
import com.timebank.volunteer.mapper.VolunteerListMapper;
import com.timebank.volunteer.mapper.VolunteerMapper;
import com.timebank.volunteer.pojo.entity.Volunteer;
import com.timebank.volunteer.pojo.entity.VolunteerAttendList;
import com.timebank.volunteer.pojo.entity.VolunteerMessage;
import com.timebank.volunteer.pojo.vo.VolunteerAttendListVO;
import com.timebank.volunteer.pojo.vo.VolunteerListVO;
import com.timebank.volunteer.pojo.vo.VolunteerVO;
import com.timebank.websocket.pojo.vo.SendMsgVO;
import com.timebank.websocket.service.RemoteWebSocketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.rmi.ServerException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 志愿服务消息 监听类
 * @author shiliuyinzhen
 */
@Component
@Slf4j
public class VolunteerMessageListener {

    @Resource
    private VolunteerListMapper volunteerListMapper;

    @Resource
    private VolunteerMapper volunteerMapper;

    @Resource
    private VolunteerAttendListMapper volunteerAttendListMapper;

    @Resource
    private RemoteWebSocketService remoteWebSocketService;

    @Resource
    private RemoteUserService remoteUserService;

    @Resource
    private RemoteTimeCoinService remoteTimeCoinService;

    @Resource
    private RemoteContractService remoteContractService;

    /**
     * 处理 志愿服务消息
     * @param msg
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues = QueueConstant.VOLUNTEER_MESSAGE_QUEUE)
    @RabbitHandler
    public void HandelVolunteerServiceMessage(String msg, Message message, Channel channel) throws IOException {
        //将json格式字符串转换为对象
        VolunteerMessage volunteerMessage = JSON.parseObject(msg, VolunteerMessage.class);
        log.info("接收到志愿服务消息：{}", volunteerMessage);
        Long getterId = volunteerMessage.getGetterId();

        Integer typeId = volunteerMessage.getTypeId();
        //如果是发布志愿服务
        if (typeId.equals(MessageTypeConstant.VOLUNTEER_INSERT)){
            if (getterId.equals(SenderAndGetterConstant.PUBLISHER)){
                //更新账户信息，创建时间币交易
                Boolean result = addVolunteerService(volunteerMessage,message,channel);
                if (result == true){
                    log.info("发布志愿服务成功！");
                }
            }
        } else if (typeId.equals(MessageTypeConstant.VOLUNTEER_UPDATE)){
            //如果是修改志愿服务
            //如果消息接受者是志愿服务参与者
            if (getterId.equals(SenderAndGetterConstant.VOLUNTEER_LIST)){
                Boolean result = sendMsgToVolunteerList(volunteerMessage, message, channel);
                if (result == true){
                    log.info("向志愿服务参与者发送 修改志愿服务 成功！");
                }
            } else {
                log.error("未知接收者，处理失败！");
            }
        } else if (typeId.equals(MessageTypeConstant.VOLUNTEER_DELETE)) {
            //如果是取消发布志愿服务
            //如果消息接受者是志愿服务参与者
            if (getterId.equals(SenderAndGetterConstant.VOLUNTEER_LIST)){
                Boolean result = deleteVolunteers(volunteerMessage,message,channel);
                if (result == true){
                    log.info("取消发布志愿服务成功！");
                }
            } else {
                log.error("未知接收者，处理失败！");
            }
        }else if (typeId.equals(MessageTypeConstant.VOLUNTEER_END)){
            //如果是结束志愿服务
            Boolean result = endVolunteerService(volunteerMessage,message,channel);
            if (result == true){
                log.info("结束志愿服务成功！");
            }
        }else if (typeId.equals(MessageTypeConstant.VOLUNTEER_APPROVE_PASS)){
            //如果是审核通过消息
            //如果接受者是发布者
            if (getterId.equals(SenderAndGetterConstant.PUBLISHER)){
                Boolean result = sendPassMsgToPublisher(volunteerMessage,message,channel);
            }
        }else if (typeId.equals(MessageTypeConstant.VOLUNTEER_APPROVE_FAIL)){
            //如果是志愿服务审核不通过
            //如果接受者是发布者
            if(getterId.equals(SenderAndGetterConstant.PUBLISHER)){
                //退还时间币
                Boolean result = returnTimeCoin(volunteerMessage,message,channel);
                if (result == true){
                    log.info("志愿服务审核不通过，退回时间币成功！");
                }
            }else {
                log.error("未知接收者，处理失败！");
            }
        }else {
            log.error("未知消息类型，处理失败！");
        }

        try {
            // RabbitMQ的ack机制中，第二个参数返回true，表示需要将这条消息投递给其他的消费者重新消费
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            // 第三个参数true，表示这个消息会重新进入队列
            //channel.basicNack(deliveryTag, false, true);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }

    /**
     * 发送志愿服务审核通过消息给发布者
     * @param volunteerMessage
     * @param message
     * @param channel
     * @return
     */
    private Boolean sendPassMsgToPublisher(VolunteerMessage volunteerMessage, Message message, Channel channel) {
        //将string转换为数组和list
        String volunteerIdStr = volunteerMessage.getVolunteerId();
        List<Long> volunteerIdsList = Arrays.asList(volunteerIdStr.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        Long[] volunteerIds = volunteerIdsList.toArray(new Long[volunteerIdsList.size()]);
        Long volunteerId = volunteerIds[0];
        //查询志愿服务
        Volunteer volunteer = volunteerMapper.getVolunteerById(volunteerId);
        //发送志愿服务审核通过消息给发布者
        //支付成功，发送到账消息
        String url = URLConstant.VOLUNTEER_SERVICE_BASE_URL + "/" + volunteerId;
        StringBuffer content = new StringBuffer();
        content.append("您发布的志愿服务:《")
                .append(volunteer.getTitle())
                .append("》审核已通过")
                .append("。")
                .append("详情可点击链接查看：")
                .append(url);
        SendMsgVO sendMsgVO = SendMsgVO.builder()
                .title("志愿服务审核通过")
                .senderId("999")
                .senderName("system")
                .getterId(volunteer.getPublisherId().toString())
                .content(content.toString())
                .volunteerId(volunteerId)
                .typeId(MessageTypeConstant.VOLUNTEER_APPROVE_PASS)
                .createTime(LocalDateTime.now().toString())
                .build();
        //发送消息
        AjaxResult ajaxResult = remoteWebSocketService.sendMessage(sendMsgVO, SecurityConstants.INNER);
        //如果发送失败
        if (ajaxResult.isError()){
            log.info("发送志愿服务审核通过消息失败！");
        }
        return true;
    }

    /**
     * 退回时间币
     * @return
     */
    private Boolean returnTimeCoin(VolunteerMessage volunteerMessage,Message message,Channel channel) throws IOException {
        //将string转换为数组和list
        String volunteerIdStr = volunteerMessage.getVolunteerId();
        List<Long> volunteerIdsList = Arrays.asList(volunteerIdStr.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        Long[] volunteerIds = volunteerIdsList.toArray(new Long[volunteerIdsList.size()]);
        //查询志愿服务
        List<Volunteer> volunteers = volunteerMapper.getVolunteerByIds(volunteerIds);
        Integer timeCoinSum = 0;
        for (Volunteer volunteer : volunteers) {
            //新增退回时间币的交易
            TimeCoinTrade timeCoinTrade = TimeCoinTrade.builder()
                    .payerId(999L)
                    .payerName("system")
                    .getterId(volunteer.getPublisherId())
                    .getterName(volunteer.getCreateBy())
                    .volunteerId(volunteer.getId())
                    .timeCoin(volunteer.getTimeCoin()*volunteer.getVolunteerNum())
                    .tradeTime(LocalDateTime.now())
                    .status(TimeCoinTradeStatusConstant.PAID)
                    .build();
            timeCoinTrade.setCreateBy("system");
            timeCoinTrade.setCreateTime(LocalDateTime.now());
            timeCoinTrade.setRemark("取消发布志愿服务，退回时间币");
            AjaxResult ajaxResult = remoteTimeCoinService.createTrade(timeCoinTrade,SecurityConstants.INNER);
            if (ajaxResult.isError()){
                throw new ServiceException("创建退回时间币交易失败");
            }
            //发送退回时间币消息
            String url = URLConstant.VOLUNTEER_SERVICE_BASE_URL + "/" + volunteer.getId();
            StringBuffer content = new StringBuffer();
            content.append("您发布的志愿服务:《")
                    .append(volunteer.getTitle())
                    .append("》审核不通过，退回时间币：")
                    .append(volunteer.getVolunteerNum()*volunteer.getTimeCoin())
                    .append("。")
                    .append("详情可点击链接查看：" + url);
            SendMsgVO sendMsgVO = SendMsgVO.builder()
                    .title("志愿服务审核不通过,退回时间币")
                    .senderId("999")
                    .senderName("system")
                    .getterId(volunteer.getPublisherId().toString())
                    .content(content.toString())
                    .volunteerId(volunteer.getId())
                    .typeId(MessageTypeConstant.TIMECOIN_TRADE_RETURN)
                    .timeCoin(volunteer.getVolunteerNum()*volunteer.getTimeCoin())
                    .build();
            ajaxResult = remoteWebSocketService.sendMessage(sendMsgVO, SecurityConstants.INNER);
            if (ajaxResult.isError()){
                throw new ServiceException("发送退回时间币消息失败");
            }
            timeCoinSum += volunteer.getTimeCoin()* volunteer.getVolunteerNum();
        }
        //原路退回时间币
        List<Long> userIds = new ArrayList<>();
        userIds.add(volunteers.get(0).getPublisherId());
        Result<List<SysUser>> result = remoteUserService.getUserByIds(userIds, SecurityConstants.INNER);
        List<SysUser> sysUsers = result.getData();
        SysUser sysUser = sysUsers.get(0);
        sysUser.setTimeCoin(sysUser.getTimeCoin() + timeCoinSum);
        //更新账本，退回时间币
        User user = User.builder()
                .userId(sysUser.getUserId())
                .userName(sysUser.getUserName())
                .timeCoin(sysUser.getTimeCoin())
                .build();
        AjaxResult ajaxResult = remoteContractService.updateContractUser(user,SecurityConstants.INNER);
        if (ajaxResult.isError()){
            throw new ServiceException("退回时间币，更新账本失败");
        }
        //更新数据库,退回时间币
        remoteUserService.payTimeCoinByIds(userIds,timeCoinSum,SecurityConstants.INNER);
        if (ajaxResult.isError()){
            throw new ServerException("发送支付成功通知失败");
        }
        return true;
    }

    /**
     * 结束志愿服务
     * @param volunteerMessage
     * @param message
     * @param channel
     * @return
     */
    private Boolean endVolunteerService(VolunteerMessage volunteerMessage,Message message,Channel channel) {
        //将string转换为list
        String volunteerIdStr = volunteerMessage.getVolunteerId();
        List<Long> volunteerIdsList = Arrays.asList(volunteerIdStr.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        Long volunteerId = volunteerIdsList.get(0);
        AjaxResult ajaxResult = remoteTimeCoinService.payTimeCoinByVolunteerId(volunteerId, SecurityConstants.INNER);
        if (ajaxResult.isError()){
            throw new ServiceException("支付时间币失败");
        }
        //查询志愿服务
        VolunteerVO volunteer = volunteerMapper.getVolunteerById(volunteerId);
        //查询志愿服务参与名单
        VolunteerAttendList volunteerAttendList = VolunteerAttendList.builder()
                        .volunteerId(volunteerId)
                        .build();
        List<VolunteerAttendListVO> attendLists = volunteerAttendListMapper.getAttendList(volunteerAttendList);
        //遍历出席名单
        List<Long> userIdList = new ArrayList<>();
        for (VolunteerAttendListVO attendList : attendLists) {
            Long userId = attendList.getUserId();
            userIdList.add(userId);
        }
        //转换为string
        String userIds = StringUtils.join(userIdList, ",");
        //支付成功，发送到账消息
        String url = URLConstant.VOLUNTEER_SERVICE_BASE_URL + "/" + volunteerId;
        StringBuffer content = new StringBuffer();
        content.append("您已完成志愿服务:《")
                .append(volunteer.getTitle())
                .append("》，时间币到账：")
                .append(volunteer.getTimeCoin())
                .append("。")
                .append("详情可点击链接查看：")
                .append(url);
        SendMsgVO sendMsgVO = SendMsgVO.builder()
                .title("完成志愿服务，时间币到账")
                .senderId(volunteerMessage.getSenderId().toString())
                .senderName(volunteerMessage.getSenderName())
                .getterId(StringUtils.join(userIds, ","))
                .content(content.toString())
                .volunteerId(volunteerId)
                .typeId(MessageTypeConstant.TIMECOIN_TRADE_GET)
                .createTime(LocalDateTime.now().toString())
                .build();
        //发送消息
        ajaxResult = remoteWebSocketService.sendMessage(sendMsgVO, SecurityConstants.INNER);
        //如果发送失败
        if (ajaxResult.isError()){
            log.info("发送志愿服务结束消息失败！");
        }
        return true;
    }

    /**
     * 更新账本，退回时间币
     * @param volunteerMessage
     * @return
     */
    private Boolean deleteVolunteers(VolunteerMessage volunteerMessage,Message message,Channel channel) throws IOException {
        //将string转换为数组和list
        String volunteerIdStr = volunteerMessage.getVolunteerId();
        List<Long> volunteerIdsList = Arrays.asList(volunteerIdStr.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        Long[] volunteerIds = volunteerIdsList.toArray(new Long[volunteerIdsList.size()]);
        //查询志愿服务
        List<Volunteer> volunteers = volunteerMapper.getVolunteerByIds(volunteerIds);
        Integer timeCoinSum = 0;
        for (Volunteer volunteer : volunteers) {
            //新增退回时间币的交易
            TimeCoinTrade timeCoinTrade = TimeCoinTrade.builder()
                    .payerId(999L)
                    .payerName("system")
                    .getterId(volunteer.getPublisherId())
                    .getterName(volunteer.getCreateBy())
                    .volunteerId(volunteer.getId())
                    .timeCoin(volunteer.getTimeCoin()*volunteer.getVolunteerNum())
                    .tradeTime(LocalDateTime.now())
                    .status(TimeCoinTradeStatusConstant.PAID)
                    .build();
            timeCoinTrade.setCreateBy("system");
            timeCoinTrade.setCreateTime(LocalDateTime.now());
            timeCoinTrade.setRemark("取消发布志愿服务，退回时间币");
            AjaxResult ajaxResult = remoteTimeCoinService.createTrade(timeCoinTrade,SecurityConstants.INNER);
            if (ajaxResult.isError()){
                throw new ServiceException("创建退回时间币交易失败");
            }
            //发送退回时间币消息
//            String url = URLConstant.TIMECOIN_TARE_BASE_URL + "/" + timeCoinTrade.getId();
            StringBuffer content = new StringBuffer();
            content.append("您发布的志愿服务:《")
                    .append(volunteer.getTitle())
                    .append("》取消成功，退回时间币：")
                    .append(volunteer.getVolunteerNum()*volunteer.getTimeCoin())
                    .append("。");
            SendMsgVO sendMsgVO = SendMsgVO.builder()
                    .title("取消发布志愿服务,退回时间币")
                    .senderId("999")
                    .senderName("system")
                    .getterId(volunteer.getPublisherId().toString())
                    .content(content.toString())
                    .volunteerId(volunteer.getId())
                    .typeId(MessageTypeConstant.TIMECOIN_TRADE_RETURN)
                    .timeCoin(volunteer.getVolunteerNum()*volunteer.getTimeCoin())
                    .build();
            ajaxResult = remoteWebSocketService.sendMessage(sendMsgVO, SecurityConstants.INNER);
            if (ajaxResult.isError()){
                throw new ServiceException("发送退回时间币消息失败");
            }
            timeCoinSum += volunteer.getTimeCoin()* volunteer.getVolunteerNum();
        }
        //原路退回时间币
        List<Long> userIds = new ArrayList<>();
        userIds.add(volunteerMessage.getSenderId());
        Result<List<SysUser>> result = remoteUserService.getUserByIds(userIds, SecurityConstants.INNER);
        List<SysUser> sysUsers = result.getData();
        SysUser sysUser = sysUsers.get(0);
        sysUser.setTimeCoin(sysUser.getTimeCoin() + timeCoinSum);
        //更新账本，退回时间币
        User user = User.builder()
                .userId(sysUser.getUserId())
                .userName(sysUser.getUserName())
                .timeCoin(sysUser.getTimeCoin())
                .build();
        AjaxResult ajaxResult = remoteContractService.updateContractUser(user,SecurityConstants.INNER);
        if (ajaxResult.isError()){
            throw new ServiceException("退回时间币，更新账本失败");
        }
        //更新数据库,退回时间币
        remoteUserService.payTimeCoinByIds(userIds,timeCoinSum,SecurityConstants.INNER);
        if (ajaxResult.isError()){
            throw new ServerException("发送支付成功通知失败");
        }
        //发送消息
        Boolean result1 = sendMsgToVolunteerList(volunteerMessage, message, channel);
        if (result1 == true){
            log.info("向志愿服务参与者发送 取消发布志愿服务 成功");
        }
        //更新志愿服务参与名单
        if(volunteerListMapper.deleteVolunteerListByVolunteerIds(volunteerIds) <= 0){
            throw new ServiceException("删除志愿服务参与名单失败");
        }
        return true;
    }

    /**
     * 更新账户信息，创建时间币交易
     * @param volunteerMessage
     * @throws ServerException
     */
    private Boolean addVolunteerService(VolunteerMessage volunteerMessage,Message message, Channel channel) throws IOException {
        //查询志愿服务信息
        VolunteerVO volunteer = volunteerMapper.getVolunteerById(Long.valueOf(volunteerMessage.getVolunteerId()));
        //扣除时间币
        Result<LoginUser> result = remoteUserService.getUserInfo(volunteer.getCreateBy(), SecurityConstants.INNER);
        SysUser sysUser = result.getData().getSysUser();
        sysUser.setTimeCoin(sysUser.getTimeCoin() - volunteer.getVolunteerNum()*volunteer.getTimeCoin());
        AjaxResult ajaxResult = remoteUserService.updateUserTimeCoin(sysUser,SecurityConstants.INNER);
        if (ajaxResult.isError()){
            //重新进入队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            throw new ServiceException("发布志愿服务，扣除时间币失败");
        }
        //创建时间币交易
        TimeCoinTrade timeCoinTrade = TimeCoinTrade.builder()
                .payerId(volunteer.getPublisherId())
                .payerName(volunteer.getCreateBy())
                .getterId(999L)
                .getterName("system")
                .volunteerId(volunteer.getId())
                .timeCoin(volunteer.getVolunteerNum()*volunteer.getTimeCoin())
                .status(TimeCoinTradeStatusConstant.PAID)
                .tradeTime(LocalDateTime.now())
                .build();
        timeCoinTrade.setCreateBy("system");
        timeCoinTrade.setCreateTime(LocalDateTime.now());
        timeCoinTrade.setRemark("发布志愿服务支付时间币");
        ajaxResult = remoteTimeCoinService.createTrade(timeCoinTrade,SecurityConstants.INNER);
        if (ajaxResult.isError()){
            throw new ServerException("发布志愿服务，创建时间币交易失败");
        }
        //更新智能合约用户信息
        User user = new User(sysUser.getUserId(),sysUser.getUserName(),sysUser.getTimeCoin());
        ajaxResult = remoteContractService.updateContractUser(user,SecurityConstants.INNER);
        if (ajaxResult.isError()){
            throw new ServerException("发布志愿服务，扣除智能合约用户时间币失败");
        }else {
            log.info("更新账户信息，创建时间币交易成功！");
        }
        //发送支付成功通知
        String url = URLConstant.VOLUNTEER_SERVICE_BASE_URL + "/" + volunteer.getId();
        StringBuffer content = new StringBuffer();
        content.append("您发布志愿服务:《")
                .append(volunteer.getTitle())
                .append("》成功，共支付时间币：")
                .append(volunteer.getVolunteerNum()*volunteer.getTimeCoin())
                .append("。")
                .append("详情可点击链接查看：")
                .append(url);
        SendMsgVO sendMsgVO = SendMsgVO.builder()
                .title("发布志愿服务,支付成功")
                .senderId("999")
                .senderName("system")
                .getterId(volunteer.getPublisherId().toString())
                .content(content.toString())
                .volunteerId(volunteer.getId())
                .typeId(MessageTypeConstant.TIMECOIN_TRADE_PAY)
                .build();
        ajaxResult = remoteWebSocketService.sendMessage(sendMsgVO, SecurityConstants.INNER);
        if (ajaxResult.isError()){
            throw new ServerException("发送支付成功通知失败！");
        }
        return true;
    }

    /**
     * 发送消息给参与者
     * @param volunteerMessage
     * @param message
     * @param channel
     * @return
     */
    public Boolean sendMsgToVolunteerList(VolunteerMessage volunteerMessage, Message message, Channel channel) throws IOException {
        String volunteerIdStr = volunteerMessage.getVolunteerId();
        //将字符串转化为list数组和Long数组
        List<Long> volunteerIdsList = Arrays.asList(volunteerIdStr.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        Long[] volunteerIds = volunteerIdsList.toArray(new Long[volunteerIdsList.size()]);
        //查询志愿服务
        List<Volunteer> volunteers = volunteerMapper.getVolunteerByIds(volunteerIds);
        System.out.println("志愿服务");
        //遍历志愿服务列表
        for (Volunteer volunteer : volunteers) {
            //查询志愿服务参与者名单
            List<VolunteerListVO> volunteerLists =
                    volunteerListMapper.getVolunteerListById(volunteer.getId());
            //如果参与者不为空
            if (volunteerLists != null && volunteerLists.size() > 0){
                //遍历得到接收者id
                List<Long> userIds = new ArrayList<>();
                for (VolunteerListVO volunteerList : volunteerLists) {
                    Long userId = volunteerList.getUserId();
                    userIds.add(userId);
                }
                //创建消息并发送
                //推送志愿服务信息已修改通知
                SendMsgVO sendMsgVO = SendMsgVO.builder()
                        .volunteerId(volunteer.getId())
                        .senderId(volunteerMessage.getSenderId().toString())
                        .senderName(volunteerMessage.getSenderName())
                        .getterId(StringUtils.join(userIds.toArray(), ","))
                        .createTime(LocalDateTime.now().toString())
                        .build();
                Integer typeId = volunteerMessage.getTypeId();
                StringBuffer content = new StringBuffer();
                //如果是修改志愿服务信息
                if (typeId.equals(MessageTypeConstant.VOLUNTEER_UPDATE)){
                    String url = URLConstant.VOLUNTEER_SERVICE_BASE_URL + "/" + volunteer.getId();
                    content.append("您报名的志愿服务：")
                            .append("《")
                            .append(volunteer.getTitle())
                            .append("》")
                            .append("部分信息已进行修改，可点击链接进行查看:")
                            .append(url);
                    sendMsgVO.setTitle("志愿服务信息已变动");
                    sendMsgVO.setContent(content.toString());
                    sendMsgVO.setTypeId(MessageTypeConstant.VOLUNTEER_UPDATE);
                } else if (typeId.equals(MessageTypeConstant.VOLUNTEER_DELETE)){
                    //如果是取消发布志愿服务
                    content.append("您报名的志愿服务：")
                            .append("《")
                            .append(volunteer.getTitle())
                            .append("》")
                            .append("已取消发布");
                    sendMsgVO.setTitle("志愿服务已取消发布");
                    sendMsgVO.setContent(content.toString());
                    sendMsgVO.setTypeId(MessageTypeConstant.VOLUNTEER_DELETE);
                }
                //发送消息
                AjaxResult ajaxResult = remoteWebSocketService.sendMessage(sendMsgVO, SecurityConstants.INNER);
                //如果发送失败
                if (ajaxResult.isError()) {
                    throw new ServerException("发送 取消发布志愿服务消息 失败");
                }
            }
        }
        return true;
    }
}
